package com.china08.yxyapi.api.manage.edu.platform.notice;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.platform.YxyHomeNotice;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.mg.NoticeModel;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.MongodbUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 新闻管理 搜索
 * 
 */
@CrossOrigin
@RestController
public class NoticeSrhController extends YxyApiBaseController {

    @Autowired
    private MongoTemplate mt;

    @ApiOperation(value = "教育局 公告列表")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/platform/notice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        Integer status = reqModel.status;
        String keyword = reqModel.keyword;
        Pageable pageable = reqModel.getPageable(Direction.DESC, "created_date");
        BasicDBObject dbObject = new BasicDBObject();
        String eduId = getProfile(request, response).getSchoolId();

        // 教育局ID
        dbObject.put("edb_id", eduId);
        if (status != null && status != -1) {
            dbObject.put("status", status);
        } else {
            List<Integer> statusList = new ArrayList<Integer>();
            statusList.add(-1);
            statusList.add(0);
            BasicDBObject notIn = new BasicDBObject("$nin", statusList);
            dbObject.put("status", notIn);
        }
        if (StringUtils.isNotBlank(keyword)) {
            Pattern queryPattern = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE);
            dbObject.put("title", queryPattern);
        }
        DBCollection dbc = mt.getCollection(mt.getCollectionName(YxyHomeNotice.class));
        Page<NoticeModel> page = MongodbUtil.getPage(dbc, dbObject, pageable, NoticeModel.class);
        List<RespModel> list = new ArrayList<NoticeSrhController.RespModel>();
        for (NoticeModel newsModel : page) {
            RespModel model = new RespModel();
            model.id = newsModel.get_id();
            model.title = newsModel.getTitle();
            model.createdDate = DateToolUtils.format(newsModel.getCreated_date(), DateToolUtils.FORMAT_SHORT_YMD);
            model.userId = newsModel.getCreated_by();
            model.status = newsModel.getStatus();
            list.add(model);
        }
        Page<RespModel> respModel = new PageImpl<NoticeSrhController.RespModel>(list, pageable,
                page.getTotalElements());
        return respModel;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // -1 为全部 1：发布 2：下线（数据库-1 为删除 ， 但是这里只查询 发布的和下线的）
        public Integer status;

        public String keyword;
    }

    protected static class RespModel {
        public String id;

        public String title;

        public String createdDate;

        // 发布人
        public String userId;

        public String publishUserNick = "";
        // 1 发布 2 下线
        public Integer status;

    }

}
